package net.xdclass.service.impl;

import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.DomainManager;
import net.xdclass.model.DomainDO;
import net.xdclass.model.vo.DomainVO;
import net.xdclass.service.DomainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DomainServiceImpl implements DomainService {

    @Autowired
    private DomainManager domainManager;

    @Override
    public List<DomainVO> listAll() {
        //查询当前登录的accountNo
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        //查询自建的
        List<DomainDO> domainDOS = domainManager.listCustomDomain(accountNo);
        //查询官方的
        List<DomainDO> domainOfficialList = domainManager.listOfficialDomain();
        //合并返回
        domainDOS.addAll(domainOfficialList);
        List<DomainVO> domainVOS = domainDOS.stream().map(obj -> this.beanProcess(obj)).collect(Collectors.toList());
        return domainVOS;
    }

    private DomainVO beanProcess(DomainDO domainDO){
        DomainVO domainVO = new DomainVO();
        BeanUtils.copyProperties(domainDO,domainVO);
        return domainVO;
    }
}
